using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using System.IO;

namespace Deepcode.Flux.Core.Services.FileSystem
{
	/// <summary>
	/// Provides an object for interrogating the filesystem - searching files etc.
	/// </summary>
	public class FileScanService
	{
		/// <summary>
		/// Searches for files matching the wildcard filemask in the path specified
		/// This returns a list of FileInfo objects that takes a little longer to 
		/// process but offers more information than the string collection versions.
		/// </summary>
		/// <param name="Path"></param>
		/// <param name="FileMask"></param>
		/// <returns></returns>
		public List<System.IO.FileInfo> GetFilesInPath( string Path, string FileMask )
		{
			List<System.IO.FileInfo> results = new List<FileInfo>();
			StringCollection files = new StringCollection();
			SearchDirectory( Path, FileMask, files, false );
			foreach (string file in files)
			{
				FileInfo fi = new FileInfo( file );
				results.Add( fi );
			}
			return results;
		}
		/// <summary>
		/// Searches for files matching the wildcard FileMask in the web path specified.
		/// The web path is resolved using Server.MapPath and passed to the standard 
		/// GetFilesInPath method.
		/// </summary>
		/// <param name="Path"></param>
		/// <param name="FileMask"></param>
		/// <returns></returns>
		public List<System.IO.FileInfo> GetFilesInWebPath(string Path, string FileMask)
		{
			string PhysicalPath = HttpContext.Current.Server.MapPath( Path );
			return GetFilesInPath( PhysicalPath, FileMask );
		}
		/// <summary>
		/// Searches for files matching the wildcard FileMask in the path specified, optionally
		/// searching in the subdirectories.
		/// </summary>
		/// <param name="Path"></param>
		/// <param name="FileMask"></param>
		/// <param name="IncludeSubDirs"></param>
		/// <returns></returns>
		public StringCollection SearchFilesInPath(string Path, string FileMask, bool IncludeSubDirs)
		{
			StringCollection files = new StringCollection();
			SearchDirectory(Path, FileMask, files, IncludeSubDirs);
			return files;
		}
		/// <summary>
		/// Searches for files matching the wildcard FileMask in the web path specified. The
		/// web path is resolved using Server.MapPath and passed to the standard SearchFilesInPath
		/// method.
		/// </summary>
		/// <param name="WebPath"></param>
		/// <param name="FileMask"></param>
		/// <param name="IncludeSubDirs"></param>
		/// <returns></returns>
		public StringCollection SearchFilesInWebPath(string WebPath, string FileMask, bool IncludeSubDirs)
		{
			string PhysicalPath = HttpContext.Current.Server.MapPath(WebPath);
			return SearchFilesInPath(PhysicalPath, FileMask, IncludeSubDirs);
		}
		/// <summary>
		/// Implementation method used by SearchFilesIn*Path. This method scans for files matching
		/// the filemask in the path specified and recurses into subdirectories as necessary.
		/// </summary>
		/// <param name="Path"></param>
		/// <param name="FileMask"></param>
		/// <param name="Files"></param>
		/// <param name="IncludeSubDirs"></param>
		private void SearchDirectory(string Path, string FileMask, StringCollection Files, bool IncludeSubDirs)
		{
			if( ! Directory.Exists(Path)) return;

			string [] filemasks = FileMask.Split(';');
			foreach (string fm in filemasks)
			{
				if( fm == "" ) continue;

				string[] files = Directory.GetFiles(Path, fm);
				foreach (string file in files)
					if (!Files.Contains(file))
						Files.Add(file);

				if (IncludeSubDirs)
				{
					string[] directories = Directory.GetDirectories(Path);
					foreach (string directory in directories)
						SearchDirectory(directory, fm, Files, IncludeSubDirs);
				}
			}
		}
	}
}
